package com.putige.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


/**
 * 自定义元数据的自动填充:动态代理
 */
@Slf4j
@Component
public class MymetaObj implements MetaObjectHandler {

    /**
     * 添加操作的自动填充,补全数据
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createUser", MyThreadLocal.getCreateUser());
        metaObject.setValue("updateUser", MyThreadLocal.getCreateUser());

    }

    /**
     * 更新,修改数据操作时的自动补全数据
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser", MyThreadLocal.getCreateUser());
    }
}
